<?php
    include ("../lib/BLL/DmLoaiMonhocBLL.php");
    $ktma="";
    $ktten="";
    $ktvt="";
    $ma="";
    $masua="";
    $ten="";
    $vt="";
    $tt="";
    $tb="";
    $btnName="Thêm";
    $lmh_bll=new LoaiMonhocBLL();
    $ds_lmh=array();
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
		if(isset($_POST['txtkey']))
		{
			$keysearch=$_POST['txtkey'];
			$where="WHERE".$lmh_bll->buildSearchQueryLike($keysearch);
        }
    }
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $madm_lmh=$_GET['ma'];
        $ds_lmh=$lmh_bll->getItemLoaiMonhoc($madm_lmh);
        $ma=$ds_lmh['madm_lmh'];
        $masua=$ds_lmh['madm_lmh'];
        $ten=$ds_lmh['ten_lmh'];
        $tt="readonly";
    }
    if(isset($_POST['btnLuu'])){
        $lmh= new DmLoaiMonHoc();
        $madm_lmh=$_POST['madm_lmh'];
        $ten_lmh=$_POST['ten_lmh'];
        if($madm_lmh!=""&&$ten_lmh!=""){
            if($btnName=="Thêm"){
                $lmh->setLoaiMonHoc($madm_lmh,$ten_lmh);
                $kq=$lmh_bll->insertLMH($lmh);
                if($kq==true){
                    header("location: chuongtrinhdaotao-loaimonhoc");
                }
            }else{
                $lmh->setLoaiMonHoc($madm_lmh,$ten_lmh);
                $kq=$lmh_bll->updateLMH($lmh);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-loaimonhoc");
                }
            }
        }
    }
    if(isset($_GET['xoama'])){
        $madm_lmh=$_GET['xoama'];
        $kq=$lmh_bll->deleteLMH($madm_lmh);
        if ($kq == true){
            header("location: chuongtrinhdaotao-loaimonhoc");
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$lmh_bll->deleteAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-loaimonhoc");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $madm_lmh){
                $kq=$lmh_bll->deleteLMH($madm_lmh);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-loaimonhoc");
                }
            }
        }
    }
    //$ds_lmh=$lmh_bll->getListLoaiMonhoc();
    
    $ds_lmh=$lmh_bll->timkiem($where);
    $totalRecords=count($ds_lmh);        
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                       
    $offset=($p-1)*$limit;
    $ds_nganh=$lmh_bll->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-loaimonhoc";
    $pages=$lmh_bll->paging($totalRecords,$limit,$url,'pagers');  

    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('ktten',$ktten);
    $smarty->assign('ktma',$ktma);
    $smarty->assign('ktvt',$ktvt);
    $smarty->assign('tt',$tt);
    $smarty->assign('tb',$tb);
    $smarty->assign('ma',$ma);
    $smarty->assign('masua',$masua);
    $smarty->assign('ten',$ten);
    $smarty->assign('ds_lmh',$ds_lmh);
    $smarty->display('chuongtrinhdaotao/loaimonhoc.tpl');
?>